﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Boxie
{
    public partial class frmLog : Form
    {
        private delegate void add_delegate(string s, int img, string err = "");
        private static add_delegate add_delegate_instance;
        public bool errorOccurred;
        private frmBoxie parent;
        private string errList;

        public frmLog(frmBoxie b)
        {
            InitializeComponent();
            parent = b;
            add_delegate_instance = new add_delegate(add);
            if (BoxieRegLib.GetSettingBool("General/WindowTopMost", true))
                this.TopMost = true;
        }

        public static void add_fromThread(string s, int img, string err = "")
        {
            object[] args = new object[3] { s, img, err };
            frmBoxie.instance.Invoke(add_delegate_instance, args);
        }

        private void frmLog_Load(object sender, EventArgs e)
        {
            this.lstLog.SmallImageList = this.imgArt;
            this.errorOccurred = false;
        }

        public void add(string s, int img, string err = "")
        {
            this.lstLog.Items.Add(s, img);
            this.lstLog.EnsureVisible(this.lstLog.Items.Count - 1);
            if (err != "")
            {
                this.lstLog.Items[this.lstLog.Items.Count - 1].ForeColor = Color.Red;
                errList += (this.lstLog.Items.Count - 1).ToString() + "|" + err + "^";
                this.errorOccurred = true;
            }
            if(img==4)
                this.lstLog.Items[this.lstLog.Items.Count - 1].ForeColor = Color.DarkGreen;
            this.lstLog.Refresh();
            this.Refresh();
        }

        private void lstLog_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                int i = this.lstLog.SelectedItems[0].Index;
                string[] arr = errList.Split('^');
                foreach (string s in arr)
                {
                    string[] a = s.Split('|');
                    if (a[0] == i.ToString())
                        MessageBox.Show("Error Details: " + a[1], "Detailed Error Contents");
                }
            }
            catch
            {
                // dont do anything here, since we dont really care
            }
        }

        private void frmLog_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }

        private void frmLog_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void frmLog_Move(object sender, EventArgs e)
        {
            this.Top = parent.Top + parent.Height + 10;
            this.Left = parent.Left;
        }
    }
}
